package cn.com.zhangwoo.microstar.homepage.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.microstar.activity.R;
import cn.com.zhangwoo.microstar.adapter.CategoryPagerAdapter;
import cn.com.zhangwoo.microstar.adapter.GalleryAdapter;
import cn.com.zhangwoo.microstar.adapter.GridViewAdapter;
import cn.com.zhangwoo.microstar.inteface.IUtilDB;
import cn.com.zhangwoo.microstar.utildb.CommonToast;
import cn.com.zhangwoo.microstar.viewpager.fragment.SipermarketFragment;
import cn.com.zhangwoo.microstar.widget.MyGallery;
/***
 * 超市
 * @author Administrator
 *
 */
public class HomeSupermarketActivity extends FragmentActivity implements OnClickListener{

	private MyGallery myGallery;
	private GalleryAdapter mGalleryAdapter;//Gallery适配器
	private LinearLayout ll_focus_indicator_container = null;
	private int preSelImgIndex = 0;
	private ViewPager mViewPager;
	private GridView mGridView;
	private TextView mTextViewTitle;
	private ImageView mImageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_supermarket);
		initView();
	}
	private void initView() {
		InitFocusIndicatorContainer();
		myGallery=(MyGallery) findViewById(R.id.supermarket_gallery);
		mGalleryAdapter = new GalleryAdapter(IUtilDB.uu,HomeSupermarketActivity.this);
		findViewById(R.id.signin_actionbar_title_img).setOnClickListener(this);
		mTextViewTitle=(TextView) findViewById(R.id.signin_actionbar_title_text);
		mTextViewTitle.setText("超市");
		mImageView=(ImageView) findViewById(R.id.signin_actionbar_xname_img);
		mImageView.setImageDrawable(getResources().getDrawable(R.drawable.android_arrow_message3));
		mImageView.setOnClickListener(this);
		mGridView=(GridView) findViewById(R.id.supermarket_gridview);
		mGridView.setAdapter(new GridViewAdapter(HomeSupermarketActivity.this,IUtilDB.uuu));
		loadData();
	}
	private void loadData() {
		myGallery.setAdapter(mGalleryAdapter);
		mViewPager=(ViewPager) findViewById(R.id.supermarket_classification_viewpager);
		myGallery.setOnItemClickListener(galleyClick1);
		myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int selIndex, long arg3) {
    				//修改上一次选中项的背景
    			    selIndex = (selIndex %IUtilDB._TOPHOMEPAGEIMG.length);
    				ImageView preSelImg = (ImageView) ll_focus_indicator_container.findViewById(preSelImgIndex);
    				preSelImg.setImageDrawable(HomeSupermarketActivity.this.getResources().getDrawable(R.drawable.ic_focus));
    				//修改当前选中项的背景
    				ImageView curSelImg = (ImageView) ll_focus_indicator_container.findViewById(selIndex);
    				curSelImg.setImageDrawable(HomeSupermarketActivity.this.getResources().getDrawable(R.drawable.ic_focus_select));
    				preSelImgIndex = selIndex;
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		mViewPager.setAdapter(new CategoryPagerAdapter(getSupportFragmentManager(),new SipermarketFragment()));

	}
	
	
	OnItemClickListener galleyClick1=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			CommonToast.showToast(HomeSupermarketActivity.this, position+"??", 1);
		}
	};
	
	 OnItemClickListener galleyClick2=new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				CommonToast.showToast(HomeSupermarketActivity.this, position+"??", 1);
			}
		};
		
		
		
		
		
		
		
		
		
		/**
		 * 加载滚动布局
		 */
		  private void InitFocusIndicatorContainer() {
		    	ll_focus_indicator_container = (LinearLayout)findViewById(R.id.ll_focus_indicator_container);	
			  	for (int i = 0; i <IUtilDB._TOPHOMEPAGEIMG.length; i++) {
			  	    ImageView localImageView = new ImageView(HomeSupermarketActivity.this);
			  	    localImageView.setId(i);
			  	    ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
			  	    localImageView.setScaleType(localScaleType);
			  	    LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
			  		    24, 24);
			  	    localImageView.setLayoutParams(localLayoutParams);
			  	    localImageView.setPadding(5, 5, 5, 5);
			  	    localImageView.setImageResource(R.drawable.ic_focus);
			  	    this.ll_focus_indicator_container.addView(localImageView);
			}
		 }
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.signin_actionbar_title_img:
				HomeSupermarketActivity.this.finish();
				break;
			case R.id.signin_actionbar_xname_img:
				
				break;
			default:
				break;
			}
			
		}
	
}
